package com.UI;


	import java.util.Calendar;

import com.Model.StudentDBModel;

	import android.app.Activity;
import android.app.AlertDialog;
	import android.app.Dialog;
	import android.content.DialogInterface;
	import android.app.TimePickerDialog;
	import android.os.Bundle;
	import android.view.View;
	import android.view.View.OnClickListener;
	import android.widget.Button;
	import android.widget.CheckBox;
	import android.widget.CompoundButton;
	import android.widget.EditText;
	import android.widget.TextView;
	import android.widget.TimePicker;
	import android.content.ContentValues;
	import android.content.Intent;
	import android.util.Log;
	import android.widget.CompoundButton;
	import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener; 
	
	
	
	/**  Author: Mohammed
	 * 
	 */
	

	public class AddCourseActivity extends Activity {
	    /** Called when the activity is first created. */
		
	    private TextView startTimeDisplay, endTimeDisplay;
	    private Button startTime,endTime;
	    String sTime, eTime;

	    private int startHour, startMinute;
	    private int endHour, endMinute;
	    String days = "";
	    
	    static final int startTIME_DIALOG_ID = 0;
	    static final int endTIME_DIALOG_ID = 1;
	    
	    EditText txtCourseWebsite;
		
		
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.add_course);
	        
	    
	        
	        // capture our View elements
	        startTimeDisplay = (TextView) findViewById(R.id.startTimeDisplay);
	        endTimeDisplay = (TextView) findViewById(R.id.endTimeDisplay);	        
	        startTime = (Button) findViewById(R.id.startTime);
	        endTime = (Button) findViewById(R.id.endTime);

	        
     		final StudentDBModel model = new StudentDBModel(this);
	        
    		final CheckBox mon_check = (CheckBox) findViewById(R.id.checkMonday);
    		final CheckBox tue_check = (CheckBox) findViewById(R.id.checkTuesday);
    		final CheckBox wed_check = (CheckBox) findViewById(R.id.checkWednesday);
    		final CheckBox thu_check = (CheckBox) findViewById(R.id.checkThursday);
    		final CheckBox fri_check = (CheckBox) findViewById(R.id.checkFriday);
    		final CheckBox sat_check = (CheckBox) findViewById(R.id.checkSaturday);
    		final CheckBox sun_check = (CheckBox) findViewById(R.id.checkSunday);
    		
    		
    		txtCourseWebsite = (EditText)findViewById(R.id.txtCourseWebsite);
    		txtCourseWebsite.setText("http://");
    		
    		

     		
     		
	        // register this activity as a button event listener
	        Button btnSubmit = (Button)findViewById(R.id.btnSubmit);
	         btnSubmit.setOnClickListener(new View.OnClickListener() {
	             public void onClick(View v) {
	                 

	         		EditText txtCourseNo = (EditText)findViewById(R.id.txtCourseNo);
	        		EditText txtCourseTitle = (EditText)findViewById(R.id.txtCourseTitle);
	        		
	        		String tempCourseNo = txtCourseNo.getText().toString().replaceAll(" ", "");
	        		String tempCourseTitle = txtCourseTitle.getText().toString().replaceAll(" ", "");
	        		
	        		
	        		if(tempCourseNo.length() == 0 ||  tempCourseTitle.length() == 0 ) {
	        	      
		        		   AlertDialog alertDialog = new AlertDialog.Builder(AddCourseActivity.this).create();	        			
		        		   alertDialog.setTitle("Warning");	        		  
		        		   alertDialog.setMessage("Course Id and Title can't be blank!");	        		  	        		  
		        		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		        		  
		        		   public void onClick(DialogInterface dialog, int which) {
	        		  
	        		       return;
	        		  
	        		  } }); 
	        		  
	        		  alertDialog.show();
	        		  
	        		}
	            	 
	        		// check that courseId is numbers only
	        		else if (!checkIfNumber(tempCourseNo)){
	        			
		        			AlertDialog alertDialog = new AlertDialog.Builder(AddCourseActivity.this).create();	        			
			        		alertDialog.setTitle("Warning");		        		  
			        		alertDialog.setMessage("Only numbers allowed for Course Id !");		        		  		        		  
			        		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			        		  
			        		public void onClick(DialogInterface dialog, int which) {
			        		  
			        		     return;
			        		  
			        	    } }); 
			        		  
			        		alertDialog.show();
	        				        			
	        		}
	        		
	        		else{
	        		// collect the course data
	        		final ContentValues coursesData = new ContentValues();
	        		
	        		// eliminate spaces for the courseId
	        		String courseNoSpace = txtCourseNo.getText().toString().replaceAll(" ", "");
	        		
	        		coursesData.put("CourseId", courseNoSpace);
	        		coursesData.put("CourseName", txtCourseTitle.getText().toString());
	        		EditText txtSectionNo = (EditText)findViewById(R.id.txtSectionNo);
	        		coursesData.put("Section", txtSectionNo.getText().toString());
	        		EditText txtCourseCredit = (EditText)findViewById(R.id.txtCourseCredit);
	        		coursesData.put("Credit", txtCourseCredit.getText().toString());
	        		EditText txtCourseEmail = (EditText)findViewById(R.id.txtCourseEmail);
	        		coursesData.put("ProfEmail", txtCourseEmail.getText().toString());
	        		coursesData.put("Website", txtCourseWebsite.getText().toString());
	        		EditText txtCourseLocation = (EditText)findViewById(R.id.txtCourseLocation);
	        		coursesData.put("Location", txtCourseLocation.getText().toString());
	        		
	        		
	        		
	        		// Checkbox functionality not implemented yet 
	        		
 
	        		          
	                if (mon_check.isChecked()) {
	                	              
	                	days = days + "Mon|"; 
	                }

	                if (tue_check.isChecked()) {
	                	
	                	days = days + "Tue|";
	                	
	                }
	                
	                if (wed_check.isChecked()) {
	                	
	                	days = days + "Wed|";
	                	 
	                }
	                
	                if (thu_check.isChecked()) {
	                	
	                	days = days + "Thu|";
   
	                }
	                
	                if (fri_check.isChecked()) {
	                	
	                	days = days + "Fri|";
	                	   
	                }
	        		
	        		
	                if (sat_check.isChecked()) {
	                	
	                	days = days + "Sat|";
	                	   
	                }
	                
	                if (sun_check.isChecked()) {
	                	
	                	days = days + "Sun|";
   
	                }
	                
	                
	        		
	        		
	                coursesData.put("Day", days);
	                
	        		sTime = pad(startHour) + ":" + pad(startMinute);
	        		eTime = pad(endHour) + ":" + pad(endMinute);
	        		
	        		
	        		coursesData.put("StartTime", sTime);
	        		coursesData.put("EndTime", eTime);

	        		
	        		// use the model to store the course data
	        		model.addNewCourses(coursesData);
	        		
	                		
	        		//once the course is added, redirect the user to the View Courses page
	        		Intent viewCourse = new Intent(AddCourseActivity.this, CourseList.class);
					startActivity(viewCourse);
					AddCourseActivity.this.finish();
	             }
	             
	           }   
	         });

	        
	        startTime.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                showDialog(startTIME_DIALOG_ID);
	            }
	        });

	        
	        endTime.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                showDialog(endTIME_DIALOG_ID);
	            }
	        });	        
	        
	        // get the current time
	        final Calendar c = Calendar.getInstance();
	        startHour = c.get(Calendar.HOUR_OF_DAY);
	        startMinute = c.get(Calendar.MINUTE);
	        
	        endHour = c.get(Calendar.HOUR_OF_DAY);
	        endMinute = c.get(Calendar.MINUTE);	        

	        // display the current date
	        updateDisplay();
	    }
	        
	    
	    
	    
	 // updates the time we display in the TextView
	    private void updateDisplay() {
	        startTimeDisplay.setText(
	            new StringBuilder()
	                    .append(pad(startHour)).append(":")
	                    .append(pad(startMinute)));
	        
	        endTimeDisplay.setText(
		            new StringBuilder()
		                    .append(pad(endHour)).append(":")
		                    .append(pad(endMinute)));	        
	        
	    }

	    private static String pad(int c) {
	        if (c >= 10)
	            return String.valueOf(c);
	        else
	            return "0" + String.valueOf(c);
	    }
	        
	 // the callback received when the user "sets" the time in the dialog
	    private TimePickerDialog.OnTimeSetListener startTimeSetListener =
	        new TimePickerDialog.OnTimeSetListener() {
	            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
	                startHour = hourOfDay;
	                startMinute = minute;
	                updateDisplay();
	            }
	        };

	        
		    private TimePickerDialog.OnTimeSetListener endTimeSetListener =
		        new TimePickerDialog.OnTimeSetListener() {
		            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		                endHour = hourOfDay;
		                endMinute = minute;
		                updateDisplay();
		            }
		        };	        
	        
	   
	        @Override
	        protected Dialog onCreateDialog(int id) {
	            switch (id) {
	            case startTIME_DIALOG_ID:
	                return new TimePickerDialog(this,
	                        startTimeSetListener, startHour, startMinute, false);
	                
	            case endTIME_DIALOG_ID:
	                return new TimePickerDialog(this,
	                        endTimeSetListener, startHour, startMinute, false);	                	                        
	            
	            }
	            return null;
	        }     
	 
	        
	        public void onBackPressed(){
	        	
	        	AddCourseActivity.this.finish();
        		Intent back = new Intent(AddCourseActivity.this, CourseList.class);
				startActivity(back);
	        	
	        }
	        
	        /**
	         * Validates if input String for courseId is a number
	         */
	        public boolean checkIfNumber(String in) {
	            
	            try {

	                Integer.parseInt(in);
	            
	            } catch (NumberFormatException ex) {
	                return false;
	            }
	            
	            return true;
	        }

	        
	}
	
	

